package furns.web;

import furns.entity.Member;
import furns.service.MemberService;
import furns.service.impl.MemberServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    private MemberService memberService = new MemberServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //如果在登录页面, 用户没有输入内容，就直接提交，后台接收到的是""
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Member member = memberService.login(new Member(null, username, password, null,null));
        if (member == null){
            //把登录错误信息,放入到request域
            req.setAttribute("msg","用户名或者密码错误");
            req.setAttribute("username",username);
            //用户不存在进行转发
            req.getRequestDispatcher("/views/member/login.jsp").forward(req,resp);
        }else {
            //登录成功
            req.getRequestDispatcher("/views/member/login_ok.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
